using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sec_scan漏洞扫描系统.Sec_Tools.Sec_Lan
{
    public partial class LanHome : Form
    {
        private Thread thread;
        private bool isRunning;
        public LanHome()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (isRunning)
            {
                return;
            }
            button1.Enabled = false;
            button2.Enabled = true;
            isRunning = true;
            thread = new Thread(new ThreadStart(ScanHosts));
            thread.Start();
        }

        private void LanHome_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
            //dataGridView1.Columns.Add("HostName", "主机名称");
            //dataGridView1.Columns.Add("HostAddress", "主机地址");
            //dataGridView1.Columns.Add("Status", "状态");
            //dataGridView1.Columns.Add("Time", "执行时间");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!isRunning)
            {
                button1.Enabled = true;
                button2.Enabled = false;
                progressBar1.Value = 100;
                return;
            }

            isRunning = false;
            thread.Join();
        }
        private void ScanHosts()
        {
            dataGridView1.Rows.Clear();

            for (int i = 1; i <= 255; i++)
            {
                if (!isRunning)
                {
                    break;
                }

                string ip = $"192.168.1.{i}";
                Ping ping = new Ping();
                PingReply pingReply = ping.Send(ip, 1000);

                if (pingReply.Status == IPStatus.Success)
                {
                    string hostName = GetHostName(ip);
                    string status = "存活";
                    string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                    object[] row = new object[] { hostName, ip, status, time };

                    // 添加行到DataGridView
                    dataGridView1.Invoke(new Action(() =>
                    {
                        dataGridView1.Rows.Add(row);
                    }));
                }

                // 更新进度条
                progressBar1.Invoke(new Action(() =>
                {
                    progressBar1.Value = (int)(((float)i / 255) * 100);
                }));
            }

            isRunning = false;
        }

        private string GetHostName(string ip)
        {
            try
            {
                return System.Net.Dns.GetHostEntry(ip).HostName;
            }
            catch
            {
                return "";
            }
        }
    }
}
